package com.tsd.jxc.fin.service.impl;

import com.tsd.common.dao.CommonParameterValueMapper;
import com.tsd.common.entity.CommonParameterValue;
import com.tsd.common.entity.CommonParameterValueExt;
import com.tsd.common.vo.BatchDeleteLogicVo;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.HlpException;
import com.tsd.jxc.fin.FinanceConstants;
import com.tsd.jxc.fin.service.JxcFinanceAccountTypeService;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: JxcFinanceAccountTypeServiceImpl
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2022/10/14 10:06
 * @Version: 1.0
 */
@Service
public class JxcFinanceAccountTypeServiceImpl extends BaseServiceImpl implements JxcFinanceAccountTypeService {

    @Resource
    private CommonParameterValueMapper commonParameterValueMapper;

    @Override
    public void saveJxcFinanceAccountType(CommonParameterValueExt record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getKey1())) {
            record.setKey1(FinanceConstants.KEY_JXC_ACCOUNT_TYPE);
        }
        super.checkEmpty(record.getKey2(), "对账类型不能为空");
        super.checkEmpty(record.getKey3(), "类型编码不能为空");
        super.checkEmpty(record.getDisplay_text(), "类型名称不能为空");
        super.checkEmpty(record.getValue(), "算术运算符不能为空");
        this.checkDuplicated(record);
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            record.setPre_set(0);
            commonParameterValueMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            commonParameterValueMapper.updateByPrimaryKeySelective(record);
        }
        String key = String.format("%s@%s", DataSourceContextHolder.getDataSourceType(), record.getKey3());
        FinanceConstants.setAccountType(key, record.getDisplay_text());
        FinanceConstants.setAccountTypeOp(key, record.getValue());
    }

    private void checkDuplicated(CommonParameterValueExt valueExt) throws Exception {
        CommonParameterValueExt params = new CommonParameterValueExt();
        params.setId(valueExt.getId());
        params.setKey1(valueExt.getKey1());
        params.setKey2(valueExt.getKey2());
        params.setKey3(valueExt.getKey3());
        Integer num = commonParameterValueMapper.checkDuplicatedObj(params);
        if (num != null) {
            throw new HlpException("已存在类型编码：" + valueExt.getKey3());
        }
        params.setKey3(null);
        params.setDisplay_text(valueExt.getDisplay_text());
        num = commonParameterValueMapper.checkDuplicatedObj(params);
        if (num != null) {
            throw new HlpException("已存在类型名称：" + valueExt.getDisplay_text());
        }
    }

    @Override
    public void deleteJxcFinanceAccountType(List<String> list, SysUser opUser) throws Exception {
        if (HlpUtils.isEmptyList(list)) {
            return;
        }
        CommonParameterValueExt params = new CommonParameterValueExt();
        params.setDr(0);
        params.setSid_list(list);
        List<CommonParameterValue> valueList = commonParameterValueMapper.selectByEntity(params);
        if (HlpUtils.isEmptyList(valueList)) {
            return;
        }
        BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo(opUser);
        logicVo.setSidList(list);
        commonParameterValueMapper.deleteLogicBySidList(logicVo);
        // 清除缓存
        List<String> values = ListUtil.map(valueList, CommonParameterValue::getKey3);
        String dataSource = DataSourceContextHolder.getDataSourceType();
        FinanceConstants.clearAccountType(dataSource, values);
        FinanceConstants.clearAccountTypeOp(dataSource, values);
    }
}
